﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
namespace T03.Controllers
{
    public class ArticlesController : Controller
    {
        //
        // GET: /Articles/


        // GET: /Articles/Index
        public ActionResult Index()
        {
            return View();
        }

        ArticlesBLL bll = new ArticlesBLL();
        // GET: /Articles/Detail/1
        public ActionResult Detail(int id)
        {
            bll.Dian(id); //浏览次数+1
            ViewBag.pingluns = bll.GetPinglunsById(id);
            //获取帖子详情
            return View(bll.GetById(id));
        }

        // GET: /Articles/AddPinglun 发表评论,处理post请求
        [HttpPost]
        public ActionResult AddPinglun(Pingluns p)
        {
            if (Session["login"] == null)
            {
                TempData["msg"] = "请先登录!";
            }
            else
            {
                p.UserID = ((Users)Session["login"]).ID;//获取登录用户ID
                p.CreateTime = DateTime.Now;//发帖时间为当前时间
                if (bll.AddPinglun(p))
                    TempData["msg"] = "发表评论成功!";
                else
                {
                    TempData["msg"] = "发表评论失败!";
                }
            }
            return RedirectToActionPermanent("/Detail/" + p.ArticleId, "Articles");
        }


        // GET: /Articles/Add 发帖
        public ActionResult Add()
        {
            return View();
        }
        // GET: /Articles/Add 发帖,处理post请求
        [HttpPost]
        public ActionResult Add(Articles a)
        {
            if (Session["login"] == null)
            {
                TempData["msg"] = "请先登录!";
                return RedirectToAction("Error", "Home");
            }
            a.UserID = ((Users)Session["login"]).ID;//获取登录用户ID
            a.Dian = 0;//浏览
            a.Zan = 0;//赞
            a.Cai = 0;//踩
            a.CreateTime = DateTime.Now;//发帖时间为当前时间
            if (bll.Add(a))
                return RedirectToAction("Index", "Home");//跳转到登录(相同控制器)
            else
                TempData["msg"] = "发帖失败!";

            return View();
        }

        // GET: /Articles/MyArticles //我的帖子
        public ActionResult MyArticles()
        {
            if (Session["login"] == null)
            {
                TempData["msg"] = "请先登录!";
                return RedirectToAction("Error", "Home");
            }
            int id = ((Users)Session["login"]).ID;
            ViewBag.list = bll.GetArticlesByUser(id);
            return View();
        }

        // GET: /Articles/Del/X  //删除帖子
        public ActionResult Del(int id)
        {
            if (bll.Del(id))
            {
                TempData["msg"] = "删除成功";
            }
            else
            {
                TempData["msg"] = "删除失败!";
            }
            return RedirectToAction("MyArticles");
        }


        // GET: /Articles/Update/X  //修改帖子
        public ActionResult Update(int id)
        {
            return View(bll.GetById(id));//Model中返回帖子详情
        }
        // GET: /Articles/Update  //修改帖子,POST请求
        [HttpPost]
        public ActionResult Update(Articles a)
        {
            if (bll.Update(a))
            {
                TempData["msg"] = "修改成功";
                return RedirectToAction("MyArticles");
            }
            else
            {
                TempData["msg"] = "修改失败";
            }
            return View(a);//Model中返回帖子详情
        }


        // GET: /Articles/Zan/X  //点赞
        public ActionResult Zan(int id,string act,string ctrl)
        {
            
            if (bll.Zan(id))
            {
                TempData["msg"] = "点赞成功";
            }
            else
            {
                TempData["msg"] = "点赞失败!";
            }
            return RedirectToAction(act, ctrl);
        }

        // GET: /Articles/Cai/X  //踩
        public ActionResult Cai(int id)
        {
            Uri u = Request.UrlReferrer;
            if (bll.Cai(id))
            {
                TempData["msg"] = "点赞成功";
            }
            else
            {
                TempData["msg"] = "点赞失败!";
            }
            return RedirectToAction("Index", "Home");
        }
    }
}
